package org.punkgrok.flightrecorder.struts;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.RequestProcessor;
import org.punkgrok.flightrecorder.TestCodeBuilder;
import org.punkgrok.flightrecorder.strutstestcase.StrutsTestCaseBuilder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: rfzabick
 * Date: May 23, 2009
 * Time: 11:54:13 AM
 */
public class ExceptionalRequestProcessor extends RequestProcessor {
	Log log = LogFactory.getLog(ExceptionalRequestProcessor.class);

    protected ActionForward processException(HttpServletRequest request,
        HttpServletResponse response, Exception exception, ActionForm form,
        ActionMapping mapping)
            throws IOException, ServletException {


		try {
			logUnitTestCode(request);
		} catch (Throwable t) {//just in case
			t.printStackTrace();
		}

		return super.processException(request, response, exception, form, mapping);
    }

	void logUnitTestCode(HttpServletRequest request) {
		TestCodeBuilder builder = getTestCodeBuilder();
		String method = builder.buildTestMethod(request);
		log.warn(method);
	}

	TestCodeBuilder getTestCodeBuilder() {
		return new StrutsTestCaseBuilder();
	}
}
